package com.shenglue.ssi.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.shenglue.ssi.domain.BuyBackDetail;


public class BuyBackDetailDaoImpl extends SqlMapClientDaoSupport implements BuyBackDetailDao {

	/**
	 * 查询某入库退货单的 所有入库退货单细表信息
	 */
	public List<BuyBackDetail> selectAllBuyBackDetail( int buyBackId ) {
		@SuppressWarnings("unchecked")
		List<BuyBackDetail> list = this.getSqlMapClientTemplate().queryForList(
				"selectAllBuyBackDetail", buyBackId );

		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 增加入库退货单细表记录
	 */
	public boolean saveBuyBackDetail( BuyBackDetail buyBackDetail ) {

		try {
			Object oRtn = this.getSqlMapClientTemplate().insert( "insertBuyBackDetail", buyBackDetail );
			// 已改正 来自网上的写法。已有文章讨论

			//插入成功 返回true
			return true;
		}
		catch ( DataAccessException e ) {
			//失败 返回false
			return false;
		}
	}

	/**
	 * 删除入库退货单细表记录
	 */
	public boolean deleteBuyBackDetail( int buyBackDetailId ) {
		int flag= this.getSqlMapClientTemplate().delete("deleteBuyBackDetailById", buyBackDetailId);
		if( flag > 0 ) {
			return true;
		}
		return false;
	}

	/**
	 * 修改入库退货单细表记录
	 */
	public boolean updateBuyBackDetail( BuyBackDetail buyBackDetail ) {
		int flag = this.getSqlMapClientTemplate().update("updateBuyBackDetail", buyBackDetail);
		if( flag > 0 ) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 为修改，根据 Id 查一条 入库退货单子表记录
	 */
	public BuyBackDetail getOneBuyBackDetailForUpdate( int buyBackDetailId ) {
		Object oBuyBackDetail = this.getSqlMapClientTemplate().
		queryForObject( "getOneBuyBackDetailForUpdate", buyBackDetailId );

		BuyBackDetail buyBackDetail = (BuyBackDetail)oBuyBackDetail;

		return buyBackDetail;

	}
}
